﻿using UnityEngine;
using System.Collections;

public class BalloonPoppingGameLogic : GameLogic {

	BalloonSpawner setupScript;
	SpriteRenderer[] Balloons;
	int amountOfBalloonsToKeep;

	// Use this for initialization
	void Start () {
		Setup ();
		CacheNeededBalloons ();

	}
	void CacheNeededBalloons()
	{
		Balloons = this.transform.Find ("_Balloons").GetComponentsInChildren<SpriteRenderer> ();
		for (int i = 0; i < Balloons.Length; i++) {
			if(Balloons[i].color == setupScript.colorToKeep)
			   {
			   			amountOfBalloonsToKeep++;
			}
		}
	}
	void Setup()
	{
		setupScript = this.gameObject.GetComponent<BalloonSpawner> ();
	}
	bool stillBalloonsLeft()
	{
		Balloons = this.transform.Find ("_Balloons").GetComponentsInChildren<SpriteRenderer> ();
		foreach (SpriteRenderer ballon in Balloons) {
			if(ballon.color != setupScript.colorToKeep)
				return true;
		}
		return false;
	}
	// Update is called once per frame
	void Update () {

		if (!stillBalloonsLeft ()) {
			if(amountOfBalloonsToKeep == Balloons.Length)
				Result(true);
			else
				Result(false);
				}
	}
}
